﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace psafth.RCTiming.Main.Pages.Dialogs
{
    /// <summary>
    /// Interaction logic for PreRaceDialog.xaml
    /// </summary>
    public partial class PreRaceDialog : Window
    {
        private Page _page;

        /// <summary>
        /// Creates a PreRaceDialog with the current RacePage.
        /// </summary>
        /// <param name="rp"></param>
        public PreRaceDialog(RacePage rp)
        {
            InitializeComponent();

            _page = rp;

            tbTitle.Text = string.Format("Snabbstart {0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
        }

        public PreRaceDialog(PracticePage pp)
        {
            InitializeComponent();

            _page = pp;

            tbTitle.Text = string.Format("Träning {0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
        }

        /// <summary>
        /// Click event for the saveBtn. Gets the values from the textboxes, saves them to the RacePage and closes the window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveBtn_Click(object sender, RoutedEventArgs e)
        {
            int duration;

            if (_page is RacePage)
            {
                RacePage racePage = (RacePage)_page;

                racePage.HeatTitle = tbTitle.Text;

                int.TryParse(tbMinutes.Text, out duration);

                if (duration > 0)
                {
                    racePage.HeatDuration = duration;
                }
                else
                {
                    racePage.HeatDuration = 20;
                }
            }
            else if (_page is PracticePage)
            {
                PracticePage practicePage = (PracticePage)_page;

                practicePage.Title = tbTitle.Text;

                int.TryParse(tbMinutes.Text, out duration);

                if (duration > 0)
                {
                    practicePage.Duration = new TimeSpan(0, duration, 0);
                }
                else
                {
                    practicePage.Duration = new TimeSpan(0, 20, 0);
                }
            }

            this.Close();
        }

        private void tbMinutes_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;

            saveBtn_Click(null, null);

            e.Handled = true;
        }

        private void tbTitle_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;

            saveBtn_Click(null, null);

            e.Handled = true;
        }
    }
}
